﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XQ.Framework.model;
using XQ.Framework.core;
using System.Web;

namespace XQ.Framework.html
{
    public class HtmlHelper
    {
        #region head
        public static string getFaviconHtml(string favicon_url)
        {
            return getFaviconHtml(favicon_url, true);
        }
        public static string getFaviconHtml(string favicon_url, bool ico)
        {
            favicon_url = favicon_url == null ? string.Empty : favicon_url;
            return string.Format("<link rel='shortcut icon' type='image/{0}' href='{1}' />"
                , ico ? "ico" : "png"
                , favicon_url);
        }
        public static string getBaseHtml(string url)
        {
            return string.Format("<base href='{0}' />", url);
        }
        public static string getTitleHtml(string title)
        {
            return string.Format("<title>{0}</title>", title);
        }
        public static string getKeyWordsHtml(string keywords)
        {
            return string.Format("<meta name='Keywords' content='{0}' />", keywords);
        }
        public static string getDescriptionHtml(string description)
        {
            return string.Format("<meta name='Description' content='{0}' />", description);
        }
        public static string getCssHtml(string css_url)
        {
            return string.Format("<link href='{0}' type='text/css' rel='stylesheet'>", css_url);
        }
        public static string getJsHtml(string js_url)
        {
            return string.Format("<script src='{0}' type='text/javascript'></script>", js_url);
        }
        public static string DoLoop(string base_url, Func<string, string> looper, params string[] strs)
        {
            if (looper == null || !ValidateHelper.IsPlumpArray(strs))
            {
                return string.Empty;
            }
            base_url = ConvertHelper.getString(base_url);
            StringBuilder html = new StringBuilder();
            for (int i = 0; i < strs.Length; ++i)
            {
                html.Append(looper(base_url + strs[i]));
            }
            return html.ToString();
        }
        #endregion

        public static string getLinkHtml(LinkModelBase model)
        {
            if (model == null) { return "error link"; }
            StringBuilder html = new StringBuilder();
            html.Append("<a");
            html.Append(ValidateHelper.IsPlumpString(model.Url) ? string.Format(" href='{0}'", model.Url) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Id) ? string.Format(" id='{0}'", model.Id) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Mclass) ? string.Format(" class='{0}'", model.Mclass) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Title) ? string.Format(" title='{0}'", model.Title) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Onclick) ? string.Format(" onclick='{0}'", model.Onclick) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Style) ? string.Format(" style='{0}'", model.Style) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Data) ? string.Format(" data='{0}'", model.Data) : string.Empty);
            html.Append(ValidateHelper.IsPlumpString(model.Target) ? string.Format(" target='{0}'", model.Target) : string.Empty);
            html.Append(string.Format(">{0}", model.Name));
            html.Append("</a>");
            return html.ToString();
        }

        public static string getPreString() { return "«"; }
        public static string getNextString() { return "»"; }

        public static string getWhiteIcon(string index)
        {
            return string.Format("<span class='wjicons_white whiteicon{0}'></span>", ConvertHelper.getString(index));
        }
        public static string getBlackIcon(string index)
        {
            return string.Format("<span class='wjicons_black blackicon{0}'></span>", ConvertHelper.getString(index));
        }
        public static string getSpanWrapper(string html)
        {
            return "<span>" + ConvertHelper.getString(html) + "</span>";
        }
        public static string HtmlEncode(string s)
        {
            return HttpUtility.HtmlEncode(ConvertHelper.getString(s));
        }
        public static string HtmlDecode(string s)
        {
            return HttpUtility.HtmlDecode(ConvertHelper.getString(s));
        }
    }
}
